package com.bm.server.serverImpl;

import com.bm.dao.Neo4jIndexDao;
import com.bm.pojo.relationship.NodeRelationship;
import com.bm.pojo.sort.AllNode;
import com.bm.server.ClassificationOfQueryServer;
import com.google.gson.Gson;
import org.neo4j.driver.Record;
import org.neo4j.driver.types.Node;
import org.neo4j.driver.types.Relationship;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: sx
 * Date: 2022/2/14
 * Time: 12:33
 * Description: java
 */
@Service
public class ClassificationOfQueryImpl implements ClassificationOfQueryServer {
    @Resource
    private Neo4jIndexDao neo4jIndexDao;

    @Resource
    private Neo4jViewActionServerImpl neo4jViewActionServer;

    @Override
    public String getSortNodeAndRelation(String query, String label, String name) {
        List<Record> sortList = neo4jIndexDao.getSortList(query, label, name);
        HashSet<AllNode> allNodes = new HashSet<>();
        List<NodeRelationship> links = new ArrayList<>();
        if(sortList.size()>0){
            for (Record r:sortList) {
                if(r.size()>1){
                    Node node1 = r.get(0).asNode();
                    AllNode start = neo4jViewActionServer.getNode(node1);
                    Relationship relationship = r.get(1).asRelationship();
                    NodeRelationship relationship1 = neo4jViewActionServer.getRelationship(relationship);
                    Node node2 = r.get(2).asNode();
                    AllNode end = neo4jViewActionServer.getNode(node2);
                    allNodes.add(start);
                    allNodes.add(end);
                    links.add(relationship1);
                }else {
                    Node node1 = r.get(0).asNode();
                    AllNode start = neo4jViewActionServer.getNode(node1);
                    allNodes.add(start);
                }

            }
        }


        Map<String, Object> hashMap = new HashMap<>();
        hashMap.put("nodes",allNodes);
        hashMap.put("links",links);

        Gson gson = new Gson();
        String json = gson.toJson(hashMap);//转成字符串

        return json;
    }


}
